package com.freewater.javabase.demo.mutithread.interrupt;

public class Interrupt2 {

    public static void main(String[] args) throws InterruptedException {
        MyThread myThread = new MyThread();
        myThread.start();
        Thread.sleep(100);
        myThread.interrupt();
        myThread.join();
        System.out.println("end");
    }

    public static class MyThread extends Thread {
        @Override
        public void run() {
            HelloThread helloThread = new HelloThread();
            helloThread.start();
            try {
                helloThread.join();
            } catch (InterruptedException e) {
                System.out.println("MyThread Interrupted");
            }
            helloThread.interrupt();
        }
    }

    public static class HelloThread extends Thread {
        @Override
        public void run() {
            int n = 0;
            while (!interrupted()) {
                System.out.println(n++ + " hello");
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    System.out.println("HelloThread Interrupted");
                    break;
                }
            }
        }
    }
}
